//
//  ZYCLoginViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/17.
//

#import "ZYCLoginViewController.h"
#import "ZYCLoginView.h"
#import "ZYCMainViewController.h"
#import "ZYCNavigationController.h"
#import "ZYCLoginRoleViewController.h"
#import "ZYCMainViewController.h"
#import "BTEnterErrorDispose.h"
#import "ThirdMacro.h"
#import "ZYCLoginRoleView.h"


@interface ZYCLoginViewController ()<UINavigationControllerDelegate,ZYCLoginViewDelegate>

@property (nonatomic, strong) ZYCLoginView *loginView;

@end

@implementation ZYCLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view addSubview:self.loginView];
//    if ([kServiceBaseUrl isEqualToString:ZYCServiceAPIEnvironmentDevelop]) {
//        self.loginView.testLabel.hidden = NO;
//    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}

- (ZYCLoginView *)loginView {
    if (!_loginView) {
        _loginView = [[ZYCLoginView alloc] initWithFrame:self.view.bounds];
        _loginView.delegate = self;
    }
    return _loginView;
}

#pragma mark - ZYCLoginViewDelegate

- (void)loginViewDidLogin{
    NSString *account = self.loginView.userName;
    if ([account isEqualToString:@"18612345678"]) {
        [[NSUserDefaults standardUserDefaults] setObject:kDevelopServiceBaseUrl forKey:@"sdfz_url"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:kReleaseServiceBaseUrl forKey:@"sdfz_url"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    [self login];
}


- (void)login{
    BTPhoneEmpty(self.loginView.userName);
    BTPhoneValidVerify(self.loginView.userName);
    BTPWDEmpty(self.loginView.password);
    NSDictionary *params = @{
        @"username":self.loginView.userName,
        @"password":self.loginView.password,
        @"loginType":@"app"
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Login_POST_Login requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {

        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSString *Authorization = responseObject[@"body"][@"token"];
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"kLoginFlag"];
                [[NSUserDefaults standardUserDefaults] setObject:Authorization forKey:@"Authorization"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [self getUserRoles];
                // 推送ID
                [self postClientId];
                
            }else{
                [MrjSVProgressHUD showErrorWithStatus:responseObject[@"message"]];
            }
        } failure:^(NSError * _Nonnull error) {
  
        }];
}



// 获取用户角色
- (void)getUserRoles{
    [[URLManager sharedURLManager] getUrlString:Login_GET_userType requestService:RequestServiceSystem parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(id  _Nullable responseObject) {
        if (SUCCESS(responseObject[@"code"])) {
            NSArray *bodyArr = responseObject[@"body"];
            if ([bodyArr containsObject:@"1"] && [bodyArr containsObject:@"2"]) {
                ZYCLoginRoleViewController *vc = [[ZYCLoginRoleViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                if ([bodyArr.firstObject isEqualToString:@"1"]) {
                    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"kUserRoleType"];
                }else{
                    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"kUserRoleType"];
                }
                [[NSUserDefaults standardUserDefaults] synchronize];
                ZYCMainViewController *mainVc = [[ZYCMainViewController alloc] init];
                [UIApplication sharedApplication].keyWindow.rootViewController = mainVc;
                [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
            }

        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}


- (void)postClientId{
    NSDictionary *params = @{
        @"clientId":[[NSUserDefaults standardUserDefaults] objectForKey:@"kClientId"],
        @"appid":kGtAppId,
        @"appkey":kGtAppKey,
        @"brand":@"ios"
    };
    [[URLManager sharedURLManager] postUrlString:Login_GET_GTClientInfo requestService:RequestServiceMessage parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                NSLog(@"保存客户端和角色的关联信息成功");
            }
        } failure:^(NSError * _Nonnull error) {

        }];

}
#pragma mark - UINavigationControllerDelegate
-(void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:[viewController isKindOfClass:[self class]] animated:YES];
}

- (void)dealloc{
    self.navigationController.delegate = nil;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
